package threadLearn.producerconsumer.v3;

import java.util.List;

public class Consumer implements Runnable{


    private List<Integer> queue;

    public Consumer(List<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while (true) {
                if (Thread.currentThread().isInterrupted())
                    break;
                Integer data = null;
                synchronized (queue) {
                    if (queue.size() <= 0) {
                        queue.wait();
                        queue.notifyAll();
                    }
                    data = queue.remove(0);
                }
                System.out.println(
                        Thread.currentThread().getId() + " 消费了:" + data);
                Thread.sleep((long) (Math.random()*1500));
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
